package com.andy.ui.widget.calendar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.RangeMonthView;

/**
 * 范围选择月视图
 * Created by huanghaibin on 2018/9/13.
 */

public class CustomRangeMonthView extends RangeMonthView {

    private float mRadiusX = 16;

    private float mRadiusY = 16;

    /**
     * 被选择的中间日期背景色
     */
    protected Paint mCenterPaint = new Paint();

    protected Paint mStartAndEndPaint = new Paint();

    protected Paint mStartAndEndTextPaint = new Paint();

    private int startDay;
    private int endDay;

    private boolean startSelect = false;

    public CustomRangeMonthView(Context context) {
        super(context);
    }


    public void setStartSelect(boolean select) {
        startSelect = select;
    }

    @Override
    protected void onPreviewHook() {
        mSchemePaint.setStyle(Paint.Style.STROKE);
        mCenterPaint.setAntiAlias(true);
        mCenterPaint.setColor(Color.parseColor("#EBF6FF"));
        mCenterPaint.setStyle(Paint.Style.FILL);
        mStartAndEndPaint.setAntiAlias(true);
        mStartAndEndPaint.setColor(Color.parseColor("#3AA7FF"));
        mStartAndEndPaint.setStyle(Paint.Style.FILL);
        mStartAndEndTextPaint.setTextSize(dipToPx(getContext(), 10));
        mStartAndEndTextPaint.setColor(Color.WHITE);
        mStartAndEndTextPaint.setStyle(Paint.Style.FILL);
        mStartAndEndTextPaint.setTextAlign(Paint.Align.CENTER);
        mSelectTextPaint.setTextSize(dipToPx(getContext(), 14));
        mSelectTextPaint.setFakeBoldText(false);
        mCurMonthTextPaint.setTextSize(dipToPx(getContext(), 14));
        mCurMonthTextPaint.setFakeBoldText(false);
        mCurMonthTextPaint.setColor(Color.parseColor("#262626"));
        mOtherMonthTextPaint.setFakeBoldText(false);
        mOtherMonthTextPaint.setTextSize(dipToPx(getContext(), 14));
    }

    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme,
                                     boolean isSelectedPre, boolean isSelectedNext) {
        if (!isSelectedPre && !isSelectedNext) {
            //重置选中位置
            startDay = 0;
            endDay = 0;
        }
        RectF rectF = new RectF();
        rectF.left = x;
        rectF.top = y;
        rectF.right = x + mItemWidth;
        rectF.bottom = y + mItemHeight;
        if (isSelectedPre) {
            if (isSelectedNext) {
                canvas.drawRect(rectF, mCenterPaint);
            } else {
                //最后一个，the last
                endDay = calendar.getDay();
                canvas.drawRect(x, y, x + mItemWidth - mRadiusX, y + mItemHeight, mCenterPaint);
                canvas.drawRoundRect(rectF, mRadiusX, mRadiusY, mStartAndEndPaint);
            }
        } else {
            if (isSelectedNext) {
                if (endDay - startDay == 1) {
                    canvas.drawRect(x + mRadiusX, y, x + mItemWidth + mRadiusX, y + mItemHeight, mCenterPaint);
                } else {
                    canvas.drawRect(x + mRadiusX, y, x + mItemWidth, y + mItemHeight, mCenterPaint);
                }
            }
            //第一个单独 选中
            if (startSelect) {
                endDay = calendar.getDay();
            } else {
                startDay = calendar.getDay();
            }
            canvas.drawRoundRect(rectF, mRadiusX, mRadiusY, mStartAndEndPaint);
        }
        return false;
    }

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y, boolean isSelected) {

    }

    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        float baselineY = mTextBaseLine + y;
        int cx = x + mItemWidth / 2;
        boolean isInRange = isInRange(calendar);
        boolean isEnable = !onCalendarIntercept(calendar);
        boolean isStart = calendar.getDay() == startDay;
        boolean isEnd = calendar.getDay() == endDay;
        if (isSelected) {
            canvas.drawText(String.valueOf(calendar.getDay()),
                    cx,
                    baselineY,
                    isStart || isEnd ? mSelectTextPaint : mCurMonthTextPaint);
            if (isStart) {
                canvas.drawText("开始",
                        cx,
                        y + mItemHeight - 10,
                        mStartAndEndTextPaint);
            } else if (isEnd) {
                canvas.drawText("结束",
                        cx,
                        y + mItemHeight - 10,
                        mStartAndEndTextPaint);
            }
        } else {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY, isInRange && isEnable ? mCurMonthTextPaint : mOtherMonthTextPaint);
        }
    }

    static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
